package com.example.datasourcedemo.utils;

import com.example.datasourcedemo.entity.Demo1;
import com.example.datasourcedemo.entity.Demo2;
import com.example.datasourcedemo.entity.DemoVo;
import com.example.datasourcedemo.entity.User;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {

    public static void main(String[] args) {


        //List<User> list = users();

        // 根据指定属性分组，并统计数量（key：指定属性，value：数量）
//        Map<Object, Long> mapGroup = list.stream().collect(Collectors.groupingBy(person -> person.getName(), Collectors.counting()));
//        System.out.println(mapGroup.toString());
//
//        // 筛选Map中value大于1的key
//        Stream<Object> stringStreamTest = mapGroup.entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey());
//        System.out.print("重复的数据：{ ");
//        stringStreamTest.forEach(str -> {
//            System.out.print(str + " ");
//            List<User> collect = list.stream().filter(user -> user.getName().equals(str)).collect(Collectors.toList());
//            for (User user : collect) {
//                System.out.println("----"+user);
//            }
//
//        });
//        System.out.println("}");

        List<DemoVo> demoVos = demoVos();

      /*  Map<String, Long> collect = demoVos.stream().collect(Collectors.groupingBy(demoVo -> demoVo.getUserName(), Collectors.counting()));
        System.out.println(collect.toString());

        Stream<Object> stringStreamTest = collect.entrySet().stream().map(entry -> entry.getKey());*/

      /*  Stream<String> stringStream = demoVos.stream().collect(Collectors.groupingBy(demoVo -> demoVo.getUserName(), Collectors.counting())).
                entrySet().stream().map(Map.Entry::getKey);
        stringStream.forEach(s -> {
            List<DemoVo> collect = demoVos.stream().filter(demoVo -> demoVo.getUserName().equals(s)).collect(Collectors.toList());
            System.out.println(collect);
            for (DemoVo demoVo : collect) {
                if (demoVo.)
            }
        });*/

        /*List<String> collect = demoVos.stream().map(DemoVo::getUserName).distinct().collect(Collectors.toList());

        List<Map<String, Object>> lsit = new ArrayList<>();*/
        /*for (String s : collect) {

            List<DemoVo> collect1 = demoVos.stream().filter(demoVo -> demoVo.getUserName().equals(s)).filter(demoVo -> demoVo.getDownReaso().equals("0")).collect(Collectors.toList());
            List<DemoVo> collect2 = demoVos.stream().filter(demoVo -> demoVo.getUserName().equals(s)).filter(demoVo -> demoVo.getDownReaso().equals("2")).collect(Collectors.toList());
            List<DemoVo> collect3 = demoVos.stream().filter(demoVo -> demoVo.getUserName().equals(s)).filter(demoVo -> demoVo.getDownReaso().equals("3")).collect(Collectors.toList());

            Map<String, Object> map = new HashMap<>();

            map.put("userName", collect1.get(0).getUserName());
            map.put("errorCount", collect2.size());
            map.put("success", collect3.size());
            lsit.add(map);
        }*/

        // 获取所有的用户名（去重复的）
        List<String> collect = demoVos.stream().map(DemoVo::getUserName).distinct().collect(Collectors.toList());
        List<Map<String, Object>> lsit = new ArrayList<>();
        collect.stream().forEach((str)->{
            List<DemoVo> collect5 = demoVos.stream().filter(demoVo -> demoVo.getUserName().equals(str)).collect(Collectors.toList());
            List<DemoVo> collect1 = collect5.stream().filter(demoVo -> demoVo.getDownReaso().equals("0")).collect(Collectors.toList());
            List<DemoVo> collect2 = collect5.stream().filter(demoVo -> demoVo.getDownReaso().equals("2")).collect(Collectors.toList());
            List<DemoVo> collect3 = collect5.stream().filter(demoVo -> demoVo.getDownReaso().equals("3")).collect(Collectors.toList());
            Optional<DemoVo> collect4 = collect5.stream().collect(Collectors.maxBy(Comparator.comparingInt(DemoVo::getMoney)));
            int money = collect4.get().getMoney();
            System.out.println("asdfasdfasdfasf"+collect4.get());

            Map<String, Object> map = new HashMap<>();
            map.put("userName", str);
            map.put("errorCount", collect2.size());
            map.put("success", collect3.size());
            lsit.add(map);
        });

        for (Map<String, Object> stringObjectMap : lsit) {
            System.out.println(stringObjectMap);
        }


    }


    public static List<User> users(){
        List<User> list = Arrays.asList(
                new User("李星云", 18, 0, "渝州",new BigDecimal(1000)),
                new User("陆林轩", 16, 1, "渝州",new BigDecimal(500)),
                new User("姬如雪", 17, 1, "幻音坊",new BigDecimal(800)),
                new User("袁天罡", 99, 0, "藏兵谷",new BigDecimal(100000)),
                new User("张子凡", 19, 0, "天师府",new BigDecimal(900)),
                new User("陆佑劫", 45, 0, "不良人",new BigDecimal(600)),
                new User("张天师", 48, 0, "天师府",new BigDecimal(1100)),
                new User("蚩梦", 181, 1, "万毒窟",new BigDecimal(800)),
                new User("蚩梦", 182, 1, "万毒窟",new BigDecimal(800)),
                new User("蚩梦", 183, 1, "万毒窟",new BigDecimal(800))
        );
        return list;
    }


    public static List<DemoVo> demoVos(){
        List<DemoVo> list = Arrays.asList(
                new Demo1("AAAA", "0", "1000", 12),
                new Demo1("BBBB",  "0", "2000", 13),
                new Demo1("CCCC", "0", "1000", 16),
                new Demo1("AAAA", "0","20000", 17),
                new Demo1("AAAA", "0", "2000", 19),
                new Demo1("BBBB", "4","2000", 37),
                new Demo1("BBBB", "3","1000", 67),
                new Demo1("BBBB", "3", "1000", 345),
                new Demo1("BBBB", "2", "2000", 45),
                new Demo1("BBBB", "2", "10000", 12)
        );
        return list;
    }



    /*flatMap(T -> Stream<R>)*/
    public static void flatmap(){
        List<String> flatmap = new ArrayList<>();
        flatmap.add("常宣灵,常昊灵");
        flatmap.add("孟婆,判官红,判官蓝");
        /*
            这里原集合中的数据由逗号分割，使用split进行拆分后，得到的是Stream<String[]>，
            字符串数组组成的流，要使用flatMap的Arrays::stream
            将Stream<String[]>转为Stream<String>,然后把流相连接
        */
        flatmap = flatmap.stream()
                .map(s -> s.split(","))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());
        for (String name : flatmap) {
            System.out.println(name);
        }
    }

    /*map(T->R)*/
    public static void map() {
        List<User> list = users();
        List<String> newlist = list.stream()
                .map(User::getName).distinct().collect(Collectors.toList());
        for (String add : newlist) {
            System.out.println(add);
        }
    }
    /**
     * 排序
     */
    public static void sorted(){

        List<User> list = users();
        List<User> collect = list.stream().sorted(Comparator.comparingInt(User::getAge)).collect(Collectors.toList());
        for (User user : collect) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

    /**
     *distinct 去重
     */
    public static void distinct(){
        List<User> list = users();
        List<User> newlist = list.stream().distinct().collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }

    /**
     * 过滤
     */
    public static void filter(){
        List<User> list = users();
        List<User> newlist = list.stream().filter(user -> user.getAge() > 20)
                .collect(Collectors.toList());
        for (User user : newlist) {
            System.out.println(user.getName()+" --> "+ user.getAge());
        }
    }
}
